var countdown = 60;
var utils = require('../../../utils/util.js') 
Page({
    data: {
        getphone:{
          phone: "",
          code: "",
          disabled: "",
          codemsg: "获取验证码"
        }
    },
    onLoad: function (options) {

    },
    editPhone: function (e) {
        this.setData({
            phone: e.detail.value
        })
        
    },
    editCode: function (e) {
        this.setData({
            code: e.detail.value
        })
        
    },
    getCode:function(e){
        var that = this;
        var phone = this.data.phone;
        var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
        if (phone == "" || !myreg.test(phone)){
            wx.showToast({
                title: '手机号有误！',
                icon: 'none',
                duration: 1500
            })  
            return false;
        }
        var data = {
            url: "/comv1/comcodephone/getcode",
            type:"newtel",
            phone: phone
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                that.setTime();
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 1500
                }) 
            }
        });
        

    },
    bindPhone:function(){
        var phone = this.data.phone;
        var code = this.data.code;
        var openid = wx.getStorageSync('openId');
        var data = {
            url: "/v1/user/add",
            openid: openid,
            type: 0,
            phone:phone,
            code: code
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                wx.setStorageSync("userinfo", res.data)
                wx.showToast({
                    title: "绑定成功",
                    icon: 'none',
                    duration: 1500
                })
                setTimeout(function(){
                    wx.navigateBack({
                        delta: 1
                    })
                },1500);
            } else {
                wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 1500
                }) 
            }
        });  
    },
    setTime:function(){//验证码
        var that = this;
        if (countdown == 0) {
            this.setData({
                disabled: "",
                codemsg: "获取验证码"
            })
            countdown = 60;
            return false;
        } else {
            this.setData({
                disabled: true,
                codemsg: "重新发送(" + countdown + ")"
            })
            countdown--;
        }
        setTimeout(function () {
            that.setTime();
        }, 1000);
    }
})